﻿using System;
using System.Text;
using StockTrader.DAL.HastcAdapter.FileParser;
using StockTrader.DAL.HastcAdapter.FileParser.Structure;
using System.Collections;

namespace StockTrader.DAL.HastcAdapter.FileMonitor.DataAccess
{
    public class DataAccessClient
    {
        public static string UpdateMARKETINFO()
        {
            DALclient dalClient = new DALclient();
            ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_MARKETINFO));

            if (list != null)
            {
                foreach (STS_MARKETINFO item in list)
                    if (item.FLOOR_CODE.Equals("02"))
                        if (dalClient.CountSTS_MARKETINFO_BYTIME(item.TIME) == 0)
                            dalClient.InsertSTS_MARKETINFO(item);

                return dalClient.CountSTS_MARKETINFO().ToString();
            }
            else
                return string.Empty;
        }

        public static string UpdateSTOCKINFO()
        {
            try
            {
                DALclient dalClient = new DALclient();
                ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_STOCKSINFO));

                int dbCount = dalClient.CountSTS_STOCKSINFO();

                foreach (STS_STOCKSINFO item in list)
                    if (item.STOCK_TYPE != 1)
                        dalClient.UpdateSecuritiesInfo(Convert.ToInt32(item.STOCK_ID), item.CODE, "HASTC");

                return dbCount.ToString();
            }
            catch (System.Exception e)
            {
                return string.Empty;
            }
        }

        public static string UpdateTOP3A()
        {
            DALclient dalClient = new DALclient();
            ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_TOP3_PRICE_A));
            int listCount = list.Count;
            int dbCount = dalClient.CountSTS_TOP3_PRICE_A();

            if ((dbCount > listCount) || (listCount == 0) || (dbCount == 0))
            {
                dalClient.DeleteSTS_TOP3_PRICE_A();
                foreach (STS_TOP3_PRICE_A item in list)
                    dalClient.InsertSTS_TOP3_PRICE_A(item);
            }
            else
            {
                //Insert only the change part!
                foreach (STS_TOP3_PRICE_A item in list.GetRange(dbCount, listCount - dbCount))
                    dalClient.InsertSTS_TOP3_PRICE_A(item);
            }

            return listCount.ToString();
        }

        public static string UpdateTOP3B()
        {
            DALclient dalClient = new DALclient();
            ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_TOP3_PRICE_B));
            int listCount = list.Count;
            int dbCount = dalClient.CountSTS_TOP3_PRICE_B();

            if ((dbCount > listCount) || (listCount == 0) || (dbCount == 0))
            {
                if (dbCount != 0)
                    dalClient.DeleteSTS_TOP3_PRICE_B();
                foreach (STS_TOP3_PRICE_B item in list)
                    dalClient.InsertSTS_TOP3_PRICE_B(item);
            }
            else if (dbCount < listCount)
            {
                //Insert only the change part!
                foreach (STS_TOP3_PRICE_B item in list.GetRange(dbCount, listCount - dbCount))
                    dalClient.InsertSTS_TOP3_PRICE_B(item);
            }
            else if (dbCount == listCount)
            {
                foreach (STS_TOP3_PRICE_B item in list)
                    dalClient.UpdateSTS_TOP3_PRICE_B(item);
            }
            return listCount.ToString();
        }

        public static string UpdateORDERS()
        {
            DALclient dalClient = new DALclient();
            ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_ORDERS));
            int listCount = list.Count;
            int dbCount = dalClient.CountSTS_ORDERS();


            if ((dbCount > listCount) || (listCount == 0) || (dbCount == 0))
            {
                if (dbCount != 0)
                    dalClient.DeleteSTS_ORDERS();
                foreach (STS_ORDERS item in list)
                    dalClient.InsertSTS_ORDERS(item);
            }
            else if (dbCount < listCount)
            {
                //Insert only the change part!
                foreach (STS_ORDERS item in list.GetRange(dbCount, listCount - dbCount))
                    dalClient.InsertSTS_ORDERS(item);
            }
            else
            {
                //Insert only the change part!
                foreach (STS_ORDERS item in list)
                    dalClient.InsertSTS_ORDERS(item);

            }
            return listCount.ToString();
        }

        public static string UpdateTRADINGRESULT()
        {
            try
            {
                DALclient dalClient = new DALclient();
                ArrayList list = FileEngineer.RetrieveFileInfo(typeof(STS_TRADING_RESULT));

                int listCount = list.Count;
                int dbCount = dalClient.CountSTS_TRADINGRESULT();

                if ((dbCount > listCount) || (listCount == 0) || (dbCount == 0))
                    if (dbCount > 0)
                        dalClient.DeleteSTS_TRADINGRESULT();

                foreach (STS_TRADING_RESULT item in list)
                    if (item.NORP.Equals(1))
                        dalClient.InsertSTS_TRADINGRESULT(item);
                    else
                        dalClient.InsertSTS_TRADINGRESULT(new STS_TRADING_RESULT(item.FLOOR_CODE, item.TRADING_DATE, item.S_ORDER_NO, item.B_ORDER_NO, item.B_ORDER_DATE, item.S_ORDER_NO, item.S_ORDER_DATE, item.B_NEXT_CNFRM, item.S_NEXT_CNFRM, item.MATCH_TIME, item.MATCH_DATE, item.B_TRADING_ID, item.S_TRADING_ID, item.B_CODE_TRADE, item.S_CODE_TRADE, item.STATUS, item.SEC_CODE, item.QUANTITY, item.PRICE, item.B_ACCOUNT_NO, item.S_ACCOUNT_NO, item.SETT_TYPE, item.SETT_DATE, item.B_PC_FLAG, item.S_PC_FLAG, item.NORP));

                return listCount.ToString();
            }
            catch (System.Exception e)
            {
                return string.Empty;
            }

        }

    }

}
